<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item prop="name" label="分类名">
        <el-input v-model="dataForm.name" placeholder="分类名"></el-input>
      </el-form-item>
      <el-form-item prop="code" label="分类编码">
        <el-input v-model="dataForm.code" placeholder="分类编码"></el-input>
      </el-form-item>
      <el-form-item prop="archiveAreaDesc" label="归档范围">
        <el-input v-model="dataForm.archiveAreaDesc" placeholder="归档范围"></el-input>
      </el-form-item>
      <el-form-item prop="parentName" :label="$t('menu.parentName')" class="menu-list">
        <el-popover v-model:visible="classListVisible" ref="classListPopover" placement="bottom-start" trigger="click" :width="400">
          <template v-slot:reference>
            <el-input v-model="dataForm.parentName" :readonly="true" placeholder="父级">
              <template v-slot:suffix> <i v-if="dataForm.pid !== '0'" @click.stop="deptListTreeSetDefaultHandle()" class="el-icon-circle-close el-input__icon"></i></template>
            </el-input>
          </template>
          <el-tree :data="classList" :props="{ label: 'name', children: 'children' }" node-key="id" ref="menuListTree" :highlight-current="true" :expand-on-click-node="false" accordion @current-change="menuListTreeCurrentChangeHandle"> </el-tree>
        </el-popover>
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item prop="deadLine" label="保管期限">
            <template v-slot="{ row }">
              <el-select v-model="dataForm.deadLine" placeholder="保管期限">
                <el-option v-for="item in dictList" :key="item.dictValue" :value="item.dictValue" :label="item.dictValue"></el-option>
              </el-select>
            </template>
          </el-form-item>
        </el-col>
        <el-col  :span="12">
          <el-form-item prop="secret" label="密级">
            <template v-slot="{ row }">
              <el-select v-model="dataForm.secret" placeholder="密级">
                <el-option v-for="item in secretList" :key="item.dictValue" :value="item.id" :label="item.dictLabel"></el-option>
              </el-select>
            </template>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item prop="archiveId" label="全宗" class="role-list">
            <el-select placeholder="请选择所属全宗" @change="handle" v-model="dataForm.archiveId">
              <el-option v-for="generalArchive in generalArchiveList" :key="generalArchive.id" :label="generalArchive.name" :value="generalArchive.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item prop="type" label="级别" class="role-list">
            <el-select placeholder="请选择门类级别" @change="handle" v-model="dataForm.type">
              <el-option v-for="type in typeList" :key="type.dictValue" :label="type.dictLabel" :value="type.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item prop="formKey" label="关联表单" class="role-list">
        <el-select placeholder="请选择关联表单" @change="handle" v-model="dataForm.formKey">
          <el-option v-for="form in formsList" :key="form" :label="form" :value="form"></el-option>
        </el-select>
      </el-form-item>

      <!--  -->
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts">
import useView from "@/hooks/useView";
import { defineComponent, reactive, toRefs } from "vue";
import baseService from "@/service/baseService";
import { useDebounce } from "@/utils/utils";
import { IObject } from "@/types/interface";
export default defineComponent({
  setup() {
    const state = reactive({
      visible: false,
      classList: [],
      classListVisible: false,
      generalArchiveList: [] as any[],
      formsList: [] as any[],
      typeList:[] as any[],
      dictList: [] as IObject,
      secretList: [] as IObject,
      generalArchiveName: "",
      dataForm: {
        type:"",
        formKey:"",
        id: "",
        archiveAreaDesc: "",
        name: "",
        pid: "0",
        deadLine: 0,
        parentName: "",
        secret: "",
        permissions: "",
        archiveId: "",
        code: ""
      }
    });
    return { ...useView(state), ...toRefs(state) };
  },
  computed: {
    dataRule() {
      return {
        name: [{ required: true, message: this.$t("validate.required"), trigger: "blur" }],
        code: [{ required: true, message: this.$t("validate.required"), trigger: "change" }],
        archiveId: [{ required: true, message: this.$t("validate.required"), trigger: "change" }],
        parentName: [{ required: true, message: this.$t("validate.required"), trigger: "change" }]
      };
    }
  },
  watch: {
    "dataForm.type"() {
      this.$refs["dataFormRef"].clearValidate();
    }
  },
  created() {
    this.dataFormSubmitHandle = useDebounce(this.dataFormSubmitHandle);
  },
  methods: {
    init(patientRow?: IObject) {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataFormRef"].resetFields();
        this.getGeneralArchiveList();
        this.getFormsList();
        this.getInfo();
        this.getDictList();
        this.getSecretDictList();
        if (patientRow) {
          this.dataForm.id = "";
          this.dataForm.pid = patientRow.id;
          this.dataForm.parentName = patientRow.name;
        } else {
          this.dataForm.parentName = "一级分类";
        }
      });
    },
    // 获取信息
    getInfo() {
      if (this.dataForm.id) {
        baseService.get(`/archive/class/${this.dataForm.id}`).then((res) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data
          };
          if (this.dataForm.pid === "0") {
            return this.deptListTreeSetDefaultHandle();
          }
          this.$refs.menuListTree.setCurrentKey(this.dataForm.pid);
        });
      }
    },
    // 上级菜单树, 设置默认值
    deptListTreeSetDefaultHandle() {
      this.dataForm.pid = "0";
      this.dataForm.parentName = "一级分类";
    },
    // 上级菜单树, 选中
    menuListTreeCurrentChangeHandle(data: IObject) {
      this.dataForm.pid = data.id;
      this.dataForm.parentName = data.name;
      this.classListVisible = false;
    },
    getFormsList() {
      baseService.get(`/archive/reorginze/form/list/all`).then((res) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        }
        if (null != res.data  && res.data.length > 0 ) {
          this.formsList = res.data;
        }
      });
    },
    getGeneralArchiveList() {
      console.info(1);
      baseService.get(`/archive/info/list`).then((res) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        }
        this.generalArchiveList = res.data;
        console.log(res);
      });
    },
    handle(v) {
      baseService.get(`/archive/class/list`, { archiveId: v }).then((res) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        }
        this.classList = res.data;
        console.log(res);
      });
    },
    getDictList() {
      baseService.get("/sys/dict/data/type/DEADLINE_MANAGE").then((res) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        }
        this.dictList = res.data;
      });
      baseService.get("/sys/dict/data/type/CATEGORY_TYPE").then((res) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        }
        this.typeList = res.data;
      });
    },
    getSecretDictList() {
      baseService.get("/sys/dict/data/type/SECURITY_CLASSIFICATION").then((res) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        }
        this.secretList = res.data;
        // this.secretList.push({ label: "", value: "" });
        // res.data.forEach((item: IObject) => this.secretList.push({ label: item.dictType, value: item.dictType }));
      });
    },
    // 表单提交
    dataFormSubmitHandle() {
      this.$refs["dataFormRef"].validate((valid: boolean) => {
        if (!valid) {
          return false;
        }

        (!this.dataForm.id ? baseService.post : baseService.put)("/archive/class", this.dataForm).then((res) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.$message({
            message: this.$t("prompt.success"),
            type: "success",
            duration: 500,
            onClose: () => {
              this.visible = false;
              this.$emit("refreshDataList");
            }
          });
        });
      });
    }
  }
});
</script>

<style lang="less">
.el-popover.el-popper {
  overflow-x: hidden;
}
.mod-sys__menu {
  .menu-list,
  .icon-list {
    .el-input__inner,
    .el-input__suffix {
      cursor: pointer;
    }
  }
  &-icon-popover {
    width: 458px !important;
    overflow-y: hidden !important;
  }
  &-icon-inner {
    width: 478px;
    max-height: 258px;
    overflow-x: hidden;
    overflow-y: auto;
  }
  &-icon-list {
    width: 458px !important;
    padding: 0;
    margin: -8px 0 0 -8px;
    > .el-button {
      padding: 8px;
      margin: 8px 0 0 8px;
      > span {
        display: inline-block;
        vertical-align: middle;
        width: 18px;
        height: 18px;
        font-size: 18px;
      }
    }
  }
}
</style>
